#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>

#include <cmath>

void drawCircle(const int numberPoints, float radius)
{
	const float angl = 2 * M_PI / numberPoints;
	float x, y;

	glBegin(GL_TRIANGLE_FAN);

		glColor3f(1.0f, 1.0f, 1.0f);
		for(int i = 0; i < numberPoints; ++i)
		{

			x = std::cos(angl * i) * radius;
			y = std::sin(angl * i) * radius;

			glVertex3f(x, y, -1);
		}

	glEnd();
}

void drawSpecialSphere(const int numberPoints)
{
	const float angl = 2 * M_PI / numberPoints;
	float x, y, z;

	glBegin(GL_POINTS);

		glColor3f(1.0f, 1.0f, 1.0f);

		glColor3f(1.0f, 0.0f, 0.0f);

		for(int i = 0; i < numberPoints; ++i)
		{
			for(int j = 0; j < numberPoints; ++j)
			{
				x = std::cos(angl * i) * std::sin(angl * j);
				y = std::sin(angl * i) * std::cos(angl * j);
				z = 1;
				//z = std::sin(angl * i) * std::sin(angl * j); ?
				//z = std::cos(angl * i) * std::cos(angl * j); ?

				glVertex3f(x, y, z);
			}
		}

	glEnd();
}

void drawUsualSphere(const int numberPoints)
{
	const float angl = 2 * M_PI / numberPoints;
	float x, y, z;

	glBegin(GL_POINTS);

		glColor3f(1.0f, 1.0f, 1.0f);

		glColor3f(1.0f, 0.0f, 0.0f);

		for(int i = 0; i < numberPoints; ++i)
		{
			for(int j = 0; j < numberPoints; ++j)
			{
				x = std::cos(angl * i);
				y = std::sin(angl * i) * std::cos(angl * j);
				z = std::sin(angl * i) * std::sin(angl * j); // ?

				glVertex3f(x, y, z);
			}
		}

	glEnd();
}
int main()
{
	sf::ContextSettings settings;
	settings.depthBits = 24;
	settings.stencilBits = 8;
	settings.antialiasingLevel = 0;
	settings.majorVersion = 3;
	settings.minorVersion = 0;

	sf::Window window(sf::VideoMode(1000, 1000), "OpenGL", sf::Style::Close, settings);
	window.setVerticalSyncEnabled(true);
	window.setFramerateLimit(10);

	window.setActive(true);

	bool running = true;
	while (running)
	{
		// handle events
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				running = false;
			}
		}

		// clear the buffers
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glPointSize(2);

		drawCircle(50, std::sqrt(2) / 2);
		drawSpecialSphere(300);

		//drawCircle(50, 1);
		//drawUsualSphere(300);

		window.display();
	}

	return 0;
}
